package com.ruoyi.project.monitor.job.task;

import com.opslab.util.EmailUtil;
import com.ruoyi.project.system.notice.domain.Notice;
import com.ruoyi.project.system.notice.service.INoticeService;
import cz.msebera.android.httpclient.NameValuePair;
import cz.msebera.android.httpclient.client.entity.UrlEncodedFormEntity;
import cz.msebera.android.httpclient.client.methods.CloseableHttpResponse;
import cz.msebera.android.httpclient.client.methods.HttpPost;
import cz.msebera.android.httpclient.entity.StringEntity;
import cz.msebera.android.httpclient.impl.client.CloseableHttpClient;
import cz.msebera.android.httpclient.impl.client.HttpClients;
import cz.msebera.android.httpclient.message.BasicNameValuePair;
import cz.msebera.android.httpclient.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

/**
 * 定时任务调度测试
 *
 * @author ruoyi
 */
@Component("ryTask")
public class RyTask {
  private static final Logger log = LoggerFactory.getLogger(RyTask.class);


  @Autowired
  private INoticeService noticeService;
  //发送通知单
  public void sendNotice(String noticeId) throws Exception{
    System.out.println("执行通知序号为" + noticeId + "的单据");
    Notice notice = null;
      notice = noticeService.selectNoticeById(Long.valueOf(noticeId));
    
    noticeService.updateNotice(notice);
  }
  //发送邮件
  public void sendEmail(String emailCode) throws Exception{
    System.out.println("给账号为" + emailCode + "的用户发邮件");
    EmailUtil se = new EmailUtil(false);
    try {
      ThreadPoolExecutor executor = new ThreadPoolExecutor(1, 1, 0, TimeUnit.MILLISECONDS, new LinkedBlockingDeque<>());
      executor.execute(new Runnable() {
        @Override
        public void run() {
          se.doSendHtmlEmail("这个是一封测试邮件", "<div>你不在学校吗？</div><br/><hr/><div>记得28号来学校</div>", emailCode);
        }
      });
      executor.shutdown();

    } catch (Exception e) {
      log.error("邮件发送失败，错误信息查看错误日志！");

    }
    //File affix = new File(path);
    //se.doSendHtmlEmail("邮件主题", "邮件内容", "17602951546@163.com", affix);//
  }
  //发送飞鸽微信信息
  public static void sendMsg(String param) throws Exception {
    String []arrs = param.split(",");
    String title = arrs[0];
    String content = arrs[1];
    String remark = arrs[2];
    // 创建一个httpclient对象
    CloseableHttpClient client = HttpClients.createDefault();
    // 创建一个post对象
    HttpPost post = new HttpPost("http://u.ifeige.cn/api/send_message");
    // 创建一个Entity，模拟表单数据
    List<NameValuePair> formList = new ArrayList<NameValuePair>();
    // 添加表单数据
    formList.add(new BasicNameValuePair("secret", "1c5dfe6075ac3babecb20bf7df163cf5"));
    formList.add(new BasicNameValuePair("token", "0fa7945f191c8fe279bfe44e8b5a58cd"));
    formList.add(new BasicNameValuePair("key", "notice"));
    formList.add(new BasicNameValuePair("title", title));
    formList.add(new BasicNameValuePair("content", content));
    formList.add(new BasicNameValuePair("remark", remark));
    formList.add(new BasicNameValuePair("time", "time()"));

    // 包装成一个Entity对象
    StringEntity entity = new UrlEncodedFormEntity(formList, "utf-8");
    // 设置请求的内容
    post.setEntity(entity);
    // 设置请求的报文头部的编码
    // post.setHeader(new BasicHeader("Content-Type",
    // "application/x-www-form-urlencoded; charset=utf-8"));
    // 设置期望服务端返回的编码
    // post.setHeader(new BasicHeader("Accept",
    // "text/plain;charset=utf-8"));
    // 执行post请求
    CloseableHttpResponse response = client.execute(post);
    // 获取响应码
    int statusCode = response.getStatusLine().getStatusCode();
    if(statusCode == 200) {
      // 获取数据
      String resStr = EntityUtils.toString(response.getEntity());
      // 输出
      System.out.println(resStr);
    } else {
      // 输出
      System.out.println(statusCode);
    }
  }

}
